/**
 * @author:albert
 * @date:2016年6月12日
 */
package com.bkbw.live.battle.business.skill.effect;

import java.util.HashMap;

import com.bkbw.live.battle.business.UnitObject;
import com.bkbw.live.battle.business.skill.EffectBase;
import com.bkbw.live.battle.database.ConstParam;
import com.bkbw.live.battle.database.DatabaseManager;
import com.bkbw.live.battle.database.bean.WeaponsBean;
import com.bkbw.live.common.GameConfig;
import com.bkbw.live.common.util.Utils;

/**
 * @author albert
 *
 */
public class SlashDamageEffect extends EffectBase {
	protected UnitObject source;
	protected UnitObject target;

	public SlashDamageEffect(HashMap<String, Object> param) {
		super(param);
		source = (UnitObject)param.get("source");
		target = (UnitObject)param.get("target");
	}
	
	@Override
	public boolean applyEffect()
	{
		WeaponsBean weapon = (WeaponsBean)source.getWeapon();
		//近战命中率=（近战武器提供的命中点数/通用参数）*（1+（知识提供的指定武器命中等级+装备提供的指定武器命中等级+BUFF技能提供的指定武器命中等级）/命中等级参数）
		float hitPer = ((float)weapon.getHitPer()*GameConfig.Percentage) * (1 + 0f / ConstParam.HitLevelPar);
		float baseEvadePer = ((float)target.getFinalEvade() * GameConfig.Percentage) + (0f) / ConstParam.EvadeLevelPar;
		float rate = hitPer * (1 - baseEvadePer);
		float randomValue = Utils.uniformFloat(0f, 1.0f);
		// 命中
		if (randomValue <= rate){
			float blockPer = 0;
			if (target.getWeapon() != null) {
				blockPer = ((float)target.getWeapon().getBlock() * GameConfig.Percentage) * (1 + 0f / ConstParam.BlockLevelPar);
			}
			float defence = ((float)target.getFinalArmor() + target.getFinalShield() + 0)*(1+0);
			defence = (defence) / (defence + 333); 
			int weaponDamage = weapon.getAttack();
			int baseDamage = (int)((float)weaponDamage * (1f + 0));
			int finalDamage = (int)(baseDamage * (1 - blockPer) * (1 - defence));
			float criPer = weapon.getCriPer() * GameConfig.Percentage;
			// 触发了暴击
			float criRand = Utils.uniformFloat(0, 1);
			if (criRand <= criPer) {
				finalDamage = (int)(finalDamage * (1 + weapon.getCriDamagePlus()*GameConfig.Percentage));
			}
			// 普通攻击
			else {
				
			}
			
			target.onHpChange(source, -finalDamage);
			source.getMap().damageNotify(source.getId(), target.getId(), finalDamage, target.getCurrentHp());
			return true;
		}
		// 闪避
		else {
			target.onHpChange(source, 0);
			source.getMap().damageNotify(source.getId(), target.getId(), 0, target.getCurrentHp());
			return true;
		}
	}
	
}
